/*
 * $Id: ChangesResultAdapterFactory.java 23 2002-09-29 13:59:30Z bpruessmann $
 *
 * Copyright (c) 2001 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.p4eclipse.internal.ui.model;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.text.MessageFormat;

import net.sourceforge.p4eclipse.core.PerforceException;
import net.sourceforge.p4eclipse.core.api.IChangesResult;
import net.sourceforge.p4eclipse.core.api.IInfoResult;
import net.sourceforge.p4eclipse.core.api.IOpenedResult;
import net.sourceforge.p4eclipse.ui.PerforceUIPlugin;
import org.eclipse.core.runtime.IAdapterFactory;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.model.IWorkbenchAdapter;

/**
  * Factory for creating IChangesResult adapter classes.
  * 
  * @version 	$Revision: 23 $
  * @author		<a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class ChangesResultAdapterFactory implements IAdapterFactory
{
	private IWorkbenchAdapter adapter = new ChangesResultAdapter();
	
	/**
	  * Convenience method for creating new instances of
	  * ChangesResultAdapterFactory.
	  */
	public static ChangesResultAdapterFactory newInstance()
	{
		return new ChangesResultAdapterFactory();
	}

	/** Creates a new ChangesResultAdapterFactory instance. */
	private ChangesResultAdapterFactory() { }		
	
	/** @see IAdapterFactory#getAdapter(Object, Class) */
	public Object getAdapter(Object adaptableObject, Class adapterType)
	{
		if ((adapterType == IWorkbenchAdapter.class) && 
		    (adaptableObject instanceof IChangesResult))
		{
			return adapter;
		}
		
		return null;
	}

	/** @see IAdapterFactory#getAdapterList() */
	public Class[] getAdapterList() 
	{ 
		return new Class[]{ IWorkbenchAdapter.class }; 
	}
	
	/**
	  * Registers the adapter objects created by this factory with the
	  * Platform plugin.
	  */
	public void register()
	{
		Platform.getAdapterManager().registerAdapters(this, IChangesResult.class);
	}	
	
	/**
	  * Adapter class for IChangesResult implementations.
	  */
	private static class ChangesResultAdapter implements IWorkbenchAdapter
	{
		/** @see IWorkbenchAdapter#getChildren(Object) */
		public Object[] getChildren(Object o) 
		{ 
			if (!(o instanceof IChangesResult))
				return null; 
								
			IOpenedResult[] result = new IOpenedResult[0];
			try
			{
				IChangesResult changesResult = (IChangesResult)o;
				result = PerforceUIPlugin.getClient().opened(changesResult.getChangelist());			
			}
			catch (PerforceException e)
			{
				PerforceUIPlugin.getPlugin().log(e.getStatus());
			}
			
			return result;
		}

		/** @see IWorkbenchAdapter#getImageDescriptor(Object) */
		public ImageDescriptor getImageDescriptor(Object o) 
		{ 
			if (!(o instanceof IChangesResult))
				return null; 

			IChangesResult changesResult = (IChangesResult)o;
						
			IInfoResult infoResult = PerforceUIPlugin.getClient().info();
			if (infoResult.getClientName().equals(changesResult.getClientName()))
				return PerforceUIPlugin.getPlugin().getImageDescriptor(PerforceUIPlugin.IMG_CLIENT_CHANGELIST);
			else
				return PerforceUIPlugin.getPlugin().getImageDescriptor(PerforceUIPlugin.IMG_OTHER_CHANGELIST);
		}

		/** @see IWorkbenchAdapter#getLabel(Object) */
		public String getLabel(Object o)
		{ 
			if (!(o instanceof IChangesResult))
				return null; 
				
			IChangesResult changesResult = (IChangesResult)o;
			String shortendDescription = cleanup(changesResult.getDescription());
			if (shortendDescription.length() >= 40)
				shortendDescription = shortendDescription.substring(0, 39) + "..."; //$NON-NLS-1$
			
			Object[] arguments = new Object[2];
			arguments[0] = changesResult.getChangelist();
			arguments[1] = shortendDescription;
				
			return MessageFormat.format("Change {0,number,#####} [{1}]", arguments);
		}

		/** @see IWorkbenchAdapter#getParent(Object) */
		public Object getParent(Object o) { return null; }
		
		/**
		  * Returns a version of text with CR/LF converted to spaces.
		  */
		private String cleanup(String text)
		{
			StringBuffer result = new StringBuffer();
			BufferedReader reader = new BufferedReader(new StringReader(text));

			try
			{
				String line = null;
				while ((line = reader.readLine()) != null)
				{
					result.append(line).append(" "); //$NON-NLS-1$
				}
				
				return result.toString().trim();
			}
			catch (IOException e)
			{
				PerforceUIPlugin.getPlugin().logError("ChangesResultAdapter.cleanup failed.", e);
			}
			
			return text;					
		}		
	}
}
